package servlets;

import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cmput391.src.queries.DBConnector;
import cmput391.src.queries.SecurityQueries;
import cmput391.src.queries.UserManagementQueries;

import  src.entities.User;

/**
 * Servlet implementation class EditForm
 */
@WebServlet("/EditForm")
public class EditForm extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditForm() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = (request.getParameter("user_name")).trim();
		
		Map<String, String> user = new HashMap<String, String>();
		ArrayList<String> doctors = new ArrayList<String>();
		ArrayList<String> allDoctors = new ArrayList<String>();
		User userInfo = null;
		
		request.setAttribute("user", user);
		request.setAttribute("doctors", doctors);
		request.setAttribute("allDoctors", allDoctors);
		
		DBConnector dbConnect = new DBConnector();
		SecurityQueries security = null;
		
		try {
			security = new SecurityQueries(dbConnect.getConnection());
			UserManagementQueries umq = new UserManagementQueries(dbConnect.getConnection(), security);
			userInfo = umq.retrieveUserData(username);
			
			user.put("username", userInfo.getUserName());
			user.put("password", userInfo.getPassword());			
			user.put("firstname", userInfo.getFirstName());
			user.put("lastname", userInfo.getLastName());
			user.put("address", userInfo.getAddress());
			user.put("email", userInfo.getEmail());
			user.put("phone", userInfo.getPhone());
			user.put("userclass", userInfo.getUserClass());
			
			doctors.addAll(umq.retrieveDoctorNames(userInfo.getUserName()));
			
			allDoctors.addAll(umq.getAllOfClass("d"));
			
			response.setHeader("Cache-Control", "no-cache");
			request.getRequestDispatcher("edit_user.jsp").forward(request, response);	
		}
		catch (Exception e) {				
		    e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
